/*
    Speedbump aka Project Kimbula
    Copyright (C) 2010  Ping Shin Ching

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see http://www.gnu.org/licenses.

	Ping Shin Ching
	righteous.ninja@gmail.com OR ping_sc@hotmail.com
	More information at http://www.tidytutorials.com/p/speedbump.html
 */



#ifndef logger_h
#define logger_h

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <semaphore.h>

typedef enum {
	LOG_CRITICAL=1,
	LOG_ERROR=3,
	LOG_WARN=7,
	LOG_INFO=15,
	LOG_DEBUG=31
}
LOGLEVEL;

class DevNull: public std::streambuf{
	int overflow(int c) { return c; }
};

class Logger{
	int globalloglevel;
	std::ostream* plogofs;
	std::ostream* devnull;
	std::ostream* accessofs;
	sem_t * access_sem;
	sem_t * log_sem;
	FILE* access_fp;

public:
	int Setuplogger(short int, const char*);
	void Closelogger();
	void FinalCleanup();
	void Logend();
	void Printlogdetails();
	void Flushlogger();
	std::ofstream* Getlogger(LOGLEVEL);
	std::ofstream* GetNullLogger();
	void CloseAccessLogger();

	int SetupAccesslogger(const char*);
	std::ofstream* GetAccesslogger();
	void CloseAccesslogger();
	void ALog(const char*, ...);
};

#define logit(x)	*proxylog.Getlogger(x)
#define lend		std::endl;proxylog.Logend()
#define logflush()	proxylog.Flushlogger()
//#define logendl		std::endl
#define logc logit(LOG_CRITICAL) 
#define loge logit(LOG_ERROR)
#define logw logit(LOG_WARN)
#define logf logit(LOG_INFO)
#define logd logit(LOG_DEBUG) 

extern Logger proxylog;

#endif
